package com.ruoyi.fantasy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.huanzhidadi.fantasy.common.constant.RedisConstants;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.domain.FantasyItemTypeBO;
import com.ruoyi.fantasy.domain.FantasyTaskRewardBO;
import com.ruoyi.fantasy.mapper.FantasyItemtypeMapper;
import com.ruoyi.fantasy.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyTaskRewardMapper;
import com.huanzhidadi.fantasy.bean.domain.FantasyTaskReward;
import com.ruoyi.fantasy.service.IFantasyTaskRewardService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 任务奖励关系 服务层实现
 * 
 * @author wanghui
 * @date 2019-07-05
 */
@Service
public class FantasyTaskRewardServiceImpl implements IFantasyTaskRewardService 
{
	@Autowired
	private FantasyTaskRewardMapper fantasyTaskRewardMapper;

	@Resource
	private FantasyItemtypeMapper fantasyItemtypeMapper;

	@Autowired
	private RedisUtil redisUtil;

	/**
     * 查询任务奖励关系信息
     * 
     * @param id 任务奖励关系ID
     * @return 任务奖励关系信息
     */
    @Override
	public FantasyTaskReward selectFantasyTaskRewardById(Long id)
	{
	    return fantasyTaskRewardMapper.selectFantasyTaskRewardById(id);
	}
	
	/**
     * 查询任务奖励关系列表
     * 
     * @param fantasyTaskReward 任务奖励关系信息
     * @return 任务奖励关系集合
     */
	@Override
	public List<FantasyTaskReward> selectFantasyTaskRewardList(FantasyTaskReward fantasyTaskReward)
	{
	    return fantasyTaskRewardMapper.selectFantasyTaskRewardList(fantasyTaskReward);
	}
	
    /**
     * 新增任务奖励关系
     * 
     * @param fantasyTaskReward 任务奖励关系信息
     * @return 结果
     */
	@Override
	public int insertFantasyTaskReward(FantasyTaskReward fantasyTaskReward)
	{
	    return fantasyTaskRewardMapper.insertFantasyTaskReward(fantasyTaskReward);
	}
	
	/**
     * 修改任务奖励关系
     * 
     * @param fantasyTaskReward 任务奖励关系信息
     * @return 结果
     */
	@Override
	public int updateFantasyTaskReward(FantasyTaskReward fantasyTaskReward)
	{
	    return fantasyTaskRewardMapper.updateFantasyTaskReward(fantasyTaskReward);
	}

	/**
     * 删除任务奖励关系对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyTaskRewardByIds(String ids)
	{
		return fantasyTaskRewardMapper.deleteFantasyTaskRewardByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<FantasyTaskRewardBO> selectAllocatedList(FantasyTaskRewardBO fantasyTaskRewardBO) {
		return fantasyTaskRewardMapper.selectAllocatedList(fantasyTaskRewardBO);
	}

	@Override
	public int deleteFantasyTaskReward(FantasyTaskRewardBO fantasyTaskRewardBO) {
		return fantasyTaskRewardMapper.deleteFantasyTaskReward(fantasyTaskRewardBO);
	}

	@Override
	public int deleteAllocRewards(String taskId, String rewardIds) {
		String[] rewardIdArray = rewardIds.split(",");
		return fantasyTaskRewardMapper.deleteAllocRewards(taskId, rewardIdArray);
	}

	@Override
	public int insertAllocRewards(String taskId, String rewardIds) {
		String[] rewardIdArray = Convert.toStrArray(rewardIds);
		// 新增任务奖励对应关系
		List<FantasyTaskReward> list = new ArrayList<>();
		for (String rewardId : rewardIdArray)
		{
			FantasyTaskReward fantasyTaskReward = new FantasyTaskReward();
			fantasyTaskReward.setId(String.valueOf(IdWorker.nextId()));
			fantasyTaskReward.setTaskId(taskId);
			fantasyTaskReward.setRewardId(rewardId);
			list.add(fantasyTaskReward);
		}
		return fantasyTaskRewardMapper.insertAllocRewards(list);
	}

	@Override
	public void transforData(List<FantasyTaskRewardBO> fantasyTaskRewardBOList) {
		Map<String, String> itemTypes = redisUtil.hmGet(RedisConstants.FANTASY_ITEM_TYPE);
		for (FantasyTaskRewardBO fantasyTaskRewardBO : fantasyTaskRewardBOList) {
			if (fantasyTaskRewardBO.getType() == 4) {
				FantasyItemType fantasyItemType = JSONObject.parseObject(itemTypes.get(fantasyTaskRewardBO.getValue()), FantasyItemType.class);
				if (null == fantasyItemType) {
					fantasyItemType = fantasyItemtypeMapper.selectFantasyItemtypeById(fantasyTaskRewardBO.getValue());
				}
				fantasyTaskRewardBO.setValue(fantasyItemType.getName());
			}
		}
	}

}
